package com.jxmobi.mediator.rest;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;

import java.net.URISyntaxException;
import java.util.List;

/**
 * @author fei <a href="mailto:xchen@jxmobi.com">Email the author</a>
 * @version 1.0 6/7/16
 */
public class RestEndpointConfig {

    private String protocol = "http";
    private String host = "testapi.bimayun.com";
    private String baseUrl;
    private Integer port;
    private BasicAuthConfig authConfig;

    public RestEndpointConfig(){}

    public RestEndpointConfig(String host, BasicAuthConfig authConfig) {
        this.host = host;
        this.authConfig = authConfig;
    }

    public RestEndpointConfig( BasicAuthConfig authConfig) {
        this.host = host;
        this.authConfig = authConfig;
    }

    public RestEndpointConfig(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public RestEndpointConfig(String host, String baseUrl) {
        this.host = host;
        this.baseUrl = baseUrl;
    }

    public RestEndpointConfig(String host, String baseUrl, Integer port) {
        this.host = host;
        this.baseUrl = baseUrl;
        this.port = port;
    }

    public URIBuilder uriBuilder(String appUrl, List<NameValuePair> params ) throws URISyntaxException {
        URIBuilder builder = new URIBuilder(getServiceUrl() + "/" + appUrl);
        builder.setParameters(params);
        return builder;
    }

    public URIBuilder uriBuilder(String appUrl ) throws URISyntaxException {
//        URIBuilder builder = new URIBuilder(getServiceUrl() + "/" + appUrl);
        URIBuilder builder = new URIBuilder(getServiceUrl() + "/" + appUrl);
        return builder;
    }

    /**
     *  get the base url
     * @return
     */
    public String getServiceUrl() {
        StringBuilder sb = new StringBuilder();
        sb.append(getProtocol()).append("://");
        sb.append(getHost());
        if(getPort()!=null) {
            sb.append(":").append(getPort());
        }
        if (getBaseUrl() != null) {
            sb.append("/").append(getBaseUrl());
        }
        return sb.toString();
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public BasicAuthConfig getAuthConfig() {
        return authConfig;
    }

    public void setAuthConfig(BasicAuthConfig authConfig) {
        this.authConfig = authConfig;
    }

    public static class BasicAuthConfig {
        private String username;

        private String password;

        public BasicAuthConfig(String username, String password) {
            this.username = username;
            this.password = password;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}
